package com.example.simin_manage.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.simin_manage.dto.PerformanceLogVO;
import com.example.simin_manage.entity.PerformanceLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @description:
 * @author: panpan
 * @time: 2025/9/21
 */
@Mapper
public interface PerformanceLogMapper extends BaseMapper<PerformanceLog> {

//    @Select("SELECT pl.* FROM performance_log pl " +
//            "JOIN team_member tm ON pl.user_id = tm.user_id " +
//            "WHERE tm.leader_id = #{leaderId} ORDER BY pl.create_time DESC")
//    List<PerformanceLog> findByLeaderId(@Param("leaderId") Long leaderId);

    @Select("<script>" +
            "SELECT l.id, l.user_id, u.nickname AS username, u.dept_id, d.name AS dept_name, " +
            "l.action, l.detail, l.create_time " +
            "FROM performance_log l " +
            "LEFT JOIN sys_user u ON l.user_id = u.id " +
            "LEFT JOIN sys_dept d ON u.dept_id = d.id " +
            "WHERE u.dept_id IN (SELECT id FROM sys_dept WHERE leader_id = #{leaderId}) " +
            "<if test='userId != null'> AND l.user_id = #{userId} </if>" +
            "<if test='startDate != null'> AND l.create_time &gt;= #{startDate} </if>" +
            "<if test='endDate != null'> AND l.create_time &lt;= #{endDate} </if>" +
            "ORDER BY l.create_time DESC" +
            "</script>")
    List<PerformanceLogVO> findByLeaderIdWithFilter(@Param("leaderId") Long leaderId,
                                                    @Param("userId") Long userId,
                                                    @Param("startDate") String startDate,
                                                    @Param("endDate") String endDate);

}
